package com.ldw.mulptiThread;

public class SimpleDeadlock {
    static final Object A = new Object();
    static final Object B = new Object();

    public static void main(String[] args) {
        // 创建线程1
        Thread thread1 = new Thread1();
        // 创建线程2
        Thread thread2 = new Thread2();

        thread1.start();
        thread2.start();
    }

    // 线程1：先持有 A，再等待 B
    static class Thread1 extends Thread {
        @Override
        public void run() {
            synchronized (A) {
                System.out.println("Thread 1: 持有 A，等待 B...");
                synchronized (B) {
                    System.out.println("Thread 1: 持有 A 和 B");
                }
            }
        }
    }

    // 线程2：先持有 B，再等待 A
    static class Thread2 extends Thread {
        @Override
        public void run() {
            synchronized (B) {
                System.out.println("Thread 2: 持有 B，等待 A...");
                synchronized (A) {
                    System.out.println("Thread 2: 持有 B 和 A");
                }
            }
        }
    }
}
